package com.kriss.crm.base;

import java.io.Serializable;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.kriss.crm.util.PageBean;

public class BaseBizImpl implements IBaseBiz {

	Logger log = LogManager.getLogger(BaseBizImpl.class);

	private IBaseDao dao;

	public Long count(String hql, Object... values) {
		log.debug(hql);
		Long result = 0L;
		try {
			result = dao.count(hql, values);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return result;
	}

	public <T> boolean delete(T t) {
		boolean flag = true;
		try {
			dao.delete(t);
		} catch (Exception e) {
			flag = false;
			log.error(e.getMessage());
		}
		return flag;
	}

	public <T> List<T> findAll(String hql, Object... values) {
		List<T> result = null;
		try {
			result = dao.findAll(hql, values);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return result;
	}

	public <T> List<T> findAll(String hql, PageBean pb, Object... values) {
		List<T> result = null;
		try {
			result = dao.findAll(hql, pb.getStart(), pb.getPageSize(), values);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return result;
	}

	public <T> T get(Class<T> cls, Serializable ID) {
		T t = null;
		try {
			t = dao.get(cls, ID);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return t;
	}

	public <T> boolean save(T t) {
		boolean flag = true;
		try {
			dao.save(t);
		} catch (Exception e) {
			flag = false;
			log.error(e.getMessage());
		}
		return flag;

	}

	public <T> boolean update(T t) {
		boolean flag = true;
		try {
			dao.update(t);
		} catch (Exception e) {
			flag = false;
			log.error(e.getMessage());
		}
		return flag;
	}

	public <T> boolean saveOrUpdate(T t) {
		
		boolean flag = true;
		try {
			dao.saveOrUpdate(t);
		} catch (Exception e) {
			flag = false;
			log.error(e.getMessage());
		}
		return flag;
		
	}
	
	public IBaseDao getDao() {
		
		
		return dao;
	}

	public void setDao(IBaseDao dao) {
		this.dao = dao;
	}

	public <T> T findOneByHql(String hql, Object... values) {

		T t = null;
		try {
			t = dao.findOneByHql(hql, values);
		} catch (Exception e) {

			e.printStackTrace();
		}
		return t;
	}



}
